using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SkinningInformation;
using Microsoft.Xna.Framework;

namespace GameEngine.Units
{
	public abstract class Animated : Doodad
	{
		private AnimatedType type;
		public Animated(AnimatedType type, World world)
			: base(type, world)
		{
			this.type = type;
			this.AnimationPlayer = new AnimationPlayer(this.type.SkinningData);
			this.PlayAnimation("Idle");
		}

		protected AnimationPlayer AnimationPlayer { get; private set; }

		// Play the first animation found from the given suggestions
		internal bool PlayAnimation(params string[] clips)
		{
			if (this.AnimationPlayer != null)
			{
				foreach (string clipName in clips)
				{
					AnimationClip clip;
					if (this.AnimationPlayer.AnimationClips.TryGetValue(clipName, out clip))
					{
						if (this.AnimationPlayer.CurrentClip != clip)
						{
							this.AnimationPlayer.StartClip(clip);
						}
						return true;
					}
				}
			}
			return false;
		}

		internal virtual void Update(GameTime gameTime)
		{
		}

		internal override void Draw(GameTime gameTime, ref Matrix view, ref Matrix projection, BoundingFrustum boundingFrustum)
		{
			// TODO: It appears that the units in the 3D are facing the wrong way... Perhaps it is something to do with the coordinate system winding or the X and Y should be swapped... Ot it is something to do with the phone being rotated...
			Matrix world = Matrix.CreateRotationZ(this.Rotation + MathHelper.PiOver2) * Matrix.CreateTranslation(this.Position);
			Render.DrawSkinnedEffectModel(gameTime, this.Model, this.AnimationPlayer, ref view, ref projection, ref world, boundingFrustum);
		}
	}
}
